﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class User_ShoppingCartHistories : System.Web.UI.Page
{
   DonDatHangCtr ddh = new DonDatHangCtr();
   public void loadBangChinh()
    {
        if (Session["UserIDU"] == null)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert",
                "alert('Vui lòng đăng nhập để xem thông tin tài khoản!');", true);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "",
                 "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'", true);   
        }
        else
        {
            MultiView1.ActiveViewIndex = 0;
            int i = dropKHChon.SelectedIndex;
            int xuly0, xuly1;
            switch (i)
            {
                case 0:
                    xuly0 = 0;
                    DataTable tbLichSu0 = ddh.getLichSuGH0(Session["UserIDU"].ToString(), xuly0);
                    if (tbLichSu0.Rows.Count > 0)
                    {
                        gvChiTietDDHang.Columns[4].Visible = true;
                        gvShowGH.DataSource = tbLichSu0;
                        gvShowGH.DataBind();
                    }
                    else
                    {
                        lbThongBao.Text = "Không Có Hoá Đơn Nào!";
                    }
                    break;
                case 1:
                    xuly1 = 0;
                    DataTable tbLichSu1 = ddh.getLichSuGH1(Session["UserIDU"].ToString(), xuly1);
                    if (tbLichSu1.Rows.Count > 0)
                    {
                        gvChiTietDDHang.Columns[4].Visible = false;
                        gvShowGH.DataSource = tbLichSu1;
                        gvShowGH.DataBind();
                    }
                    else
                    {
                        lbThongBao.Text = "Không Có Hoá Đơn Nào!";
                    }
                    break;
        }
        }

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (hfStt.Value == "1")
        {
            string mahd = hfSoHD.Value;//gvShowGH.SelectedRow.Cells[0].Text;
            DataTable dtCTHD = ddh.LayChiTietHoadon_KH(int.Parse(mahd));
            gvChiTietDDHang.DataSource = dtCTHD;
            gvChiTietDDHang.DataBind();
            EDonDatHang dtDDH = ddh.LayChiTietHoadon_KHLB(int.Parse(mahd));
            lbGiamGia.Text = dtDDH.GiamGia.ToString();
            lbTongTien.Text = dtDDH.TongTien;
            lbSoHoaDon.Text = gvShowGH.SelectedRow.Cells[0].Text;
            MultiView1.ActiveViewIndex = 1;
        }
        else
        {
            loadBangChinh();
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void gvShowGH_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvShowGH.PageIndex = e.NewPageIndex;
        loadBangChinh();
    }
    protected void gvShowGH_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lb = (Label)e.Row.FindControl("Label2");
            switch (lb.Text)
            {
                case "0":
                    lb.Text = "Chưa duyệt";
                    break;
                case "1":
                    lb.Text = "Đã duyệt";
                    break;
                case "2":
                    lb.Text = "Đã thanh toán";
                    break;
                case "3":
                    lb.Text = "Đã gửi hàng";
                    break;
                case "4":
                    lb.Text = "Nhận tại cửa hàng";
                    break;
                case "5":
                    lb.Text = "Giao dịch thành công";
                    break;
                case "6":
                    lb.Text = "Huỷ giao hàng";
                    break;
            }
        }
        
    }
    protected void gvShowGH_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        string mahd = gvShowGH.SelectedRow.Cells[0].Text;
        DataTable dtCTHD = ddh.LayChiTietHoadon_KH(int.Parse(mahd));
        gvChiTietDDHang.DataSource = dtCTHD;
        gvChiTietDDHang.DataBind();
        EDonDatHang dtDDH = ddh.LayChiTietHoadon_KHLB(int.Parse(mahd));
        lbGiamGia.Text = dtDDH.GiamGia.ToString();
        lbTongTien.Text = dtDDH.TongTien;
        lbSoHoaDon.Text = gvShowGH.SelectedRow.Cells[0].Text;
        hfStt.Value = "1";
        hfSoHD.Value = mahd;
    }
    protected void gvChiTietDDHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int mahd = int.Parse(lbSoHoaDon.Text);
        HiddenField hidden = (HiddenField)gvChiTietDDHang.Rows[e.RowIndex].Cells[3].FindControl("hdMaSP");
        if (ddh.xoadondathang_KH(mahd, hidden.Value))
        {
            hfStt.Value = "1";
            MultiView1.ActiveViewIndex = 1;
        }
        else
        {
            MultiView1.ActiveViewIndex = 0;
            hfStt.Value = "0";
        }
        //loadBangChinh();
        Response.Redirect(Request.RawUrl);
        //MultiView1.ActiveViewIndex = 1;
        
    }
    protected void dropKHChon_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadBangChinh();
    }
    protected void gvChiTietDDHang_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = (ImageButton)e.Row.Cells[4].Controls[0];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "if (!confirm('Bạn có chắc chắn xóa đơn đặt hàng " + e.Row.Cells[4].Text + "này không?')) { return false; }";
        }
    }
    protected void btnQuayLai_Click(object sender, EventArgs e)
    {
        hfStt.Value = "0";
        Response.Redirect(Request.RawUrl);
    }
}